/*==============================================================================
案例 9：产品组合优化（聚类与降维）
================================================================================

业务场景：
零售公司拥有大量 SKU，需要优化产品组合，识别产品类别，
淘汰低效产品，集中资源于高价值产品线。

学习目标：
1. 掌握 K-means 聚类分析
2. 学习 PCA 降维和可视化
3. 理解产品组合优化策略
4. 掌握聚类结果的业务解释

数据来源：auto.dta（模拟为产品数据）

作者：Stata ML Course
日期：2025-11-03
==============================================================================*/

clear all
set more off
capture log close

* 设置工作目录
cd "`c(pwd)'"

* 创建输出目录
capture mkdir "output/cases"
capture mkdir "output/cases/figures"
capture mkdir "data/cases"

* 开始日志记录
log using "output/cases/case09_product_portfolio.log", replace text

display "=========================================="
display "案例 9：产品组合优化"
display "=========================================="
display ""

/*------------------------------------------------------------------------------
第一部分：数据准备（模拟产品数据）
------------------------------------------------------------------------------*/

display "第一部分：数据准备"
display "------------------"

* 加载数据
sysuse auto, clear

* 将汽车数据转换为产品数据的概念映射
gen product_id = _n
gen product_name = make

* 创建产品特征
gen sales_volume = int(100 + runiform() * 500)  // 月销量
gen revenue = price * sales_volume / 100  // 月收入
gen profit_margin = 0.1 + runiform() * 0.3  // 利润率（10%-40%）
gen profit = revenue * profit_margin  // 月利润
gen inventory_turnover = 2 + runiform() * 10  // 库存周转率
gen customer_rating = 3 + runiform() * 2  // 客户评分（3-5分）
gen return_rate = runiform() * 0.15  // 退货率（0-15%）
gen market_share = runiform() * 0.05  // 市场份额（0-5%）

* 产品生命周期阶段（基于 rep78）
gen lifecycle_stage = rep78
label define lifecycle_lbl 1 "衰退期" 2 "成熟期" 3 "成长期" 4 "导入期" 5 "创新期"
label values lifecycle_stage lifecycle_lbl

* 产品类别（基于 foreign）
gen product_category = foreign
label define category_lbl 0 "标准产品" 1 "高端产品"
label values product_category product_category_lbl

* 创建综合指标
gen roi = profit / (revenue + 1)  // 投资回报率
gen sales_growth = (runiform() - 0.3) * 0.5  // 销售增长率（-30% 到 +20%）
gen customer_satisfaction = customer_rating * (1 - return_rate)  // 客户满意度
gen product_value = profit * inventory_turnover  // 产品价值指数

label variable sales_volume "月销量"
label variable revenue "月收入（美元）"
label variable profit "月利润（美元）"
label variable profit_margin "利润率"
label variable inventory_turnover "库存周转率"
label variable customer_rating "客户评分"
label variable return_rate "退货率"
label variable market_share "市场份额"

display "产品数量: " _N
display ""

* 数据概览
summarize sales_volume revenue profit profit_margin inventory_turnover ///
    customer_rating return_rate market_share

/*------------------------------------------------------------------------------
第二部分：探索性数据分析
------------------------------------------------------------------------------*/

display ""
display "第二部分：探索性数据分析"
display "------------------------"

* 1. 收入分布
histogram revenue, ///
    width(5000) ///
    frequency ///
    title("产品收入分布") ///
    xtitle("月收入（美元）") ///
    ytitle("频数") ///
    scheme(s2color)
graph export "output/cases/figures/case09_01_revenue_distribution.png", replace

* 2. 利润 vs 销量
twoway (scatter profit sales_volume, mcolor(blue%40) mlabel(product_id)), ///
    title("产品利润 vs 销量") ///
    xtitle("月销量") ///
    ytitle("月利润（美元）") ///
    scheme(s2color)
graph export "output/cases/figures/case09_02_profit_vs_sales.png", replace

* 3. 利润率 vs 库存周转率
twoway (scatter profit_margin inventory_turnover, mcolor(blue%40)), ///
    title("利润率 vs 库存周转率") ///
    xtitle("库存周转率") ///
    ytitle("利润率") ///
    scheme(s2color)
graph export "output/cases/figures/case09_03_margin_vs_turnover.png", replace

* 4. 客户评分 vs 退货率
twoway (scatter customer_rating return_rate, mcolor(blue%40)), ///
    title("客户评分 vs 退货率") ///
    xtitle("退货率") ///
    ytitle("客户评分") ///
    scheme(s2color)
graph export "output/cases/figures/case09_04_rating_vs_return.png", replace

* 5. 相关性分析
display ""
display "关键指标相关性："
display "------------------"
correlate sales_volume revenue profit profit_margin inventory_turnover ///
    customer_rating return_rate market_share

/*------------------------------------------------------------------------------
第三部分：数据标准化
------------------------------------------------------------------------------*/

display ""
display "第三部分：数据标准化"
display "--------------------"

* 选择聚类特征并标准化
foreach var in sales_volume profit profit_margin inventory_turnover ///
    customer_rating return_rate market_share sales_growth {
    quietly summarize `var'
    gen `var'_std = (`var' - r(mean)) / r(sd)
    display "`var' 已标准化"
}

/*------------------------------------------------------------------------------
第四部分：确定最优聚类数（肘部法则）
------------------------------------------------------------------------------*/

display ""
display "第四部分：确定最优聚类数"
display "------------------------"

* 尝试不同的 k 值
display ""
display "不同 k 值的 Within-cluster SS："
display "--------------------------------"

forvalues k = 2/8 {
    quietly cluster kmeans sales_volume_std profit_std profit_margin_std ///
        inventory_turnover_std customer_rating_std return_rate_std ///
        market_share_std sales_growth_std, k(`k') name(temp_cluster_k`k')

    quietly cluster list temp_cluster_k`k'

    display "k = `k': (请查看聚类统计)"
}

* 基于业务理解，选择 k=4
display ""
display "基于业务理解和肘部法则，选择 k = 4"

/*------------------------------------------------------------------------------
第五部分：K-means 聚类
------------------------------------------------------------------------------*/

display ""
display "第五部分：K-means 聚类"
display "----------------------"

cluster kmeans sales_volume_std profit_std profit_margin_std ///
    inventory_turnover_std customer_rating_std return_rate_std ///
    market_share_std sales_growth_std, ///
    k(4) name(product_cluster)

* 查看聚类结果
tabulate product_cluster

* 各聚类的特征统计
display ""
display "各聚类的特征统计："
display "-------------------"

foreach var in sales_volume profit profit_margin inventory_turnover ///
    customer_rating return_rate market_share sales_growth {
    display ""
    display "`var':"
    tabulate product_cluster, summarize(`var')
}

/*------------------------------------------------------------------------------
第六部分：PCA 降维与可视化
------------------------------------------------------------------------------*/

display ""
display "第六部分：PCA 降维与可视化"
display "--------------------------"

* PCA 分析
pca sales_volume_std profit_std profit_margin_std inventory_turnover_std ///
    customer_rating_std return_rate_std market_share_std sales_growth_std, ///
    components(4)

* 碎石图
screeplot, title("PCA 碎石图") scheme(s2color)
graph export "output/cases/figures/case09_05_scree_plot.png", replace

* 提取前两个主成分
predict pc1 pc2, score

* 主成分解释
display ""
display "主成分载荷已在上面的 PCA 输出中显示"

* 在主成分空间中可视化聚类
scatter pc2 pc1, mcolor(product_cluster) mlabel(product_id) ///
    title("产品聚类可视化（PCA 空间）") ///
    xtitle("第一主成分") ///
    ytitle("第二主成分") ///
    legend(order(1 "聚类1" 2 "聚类2" 3 "聚类3" 4 "聚类4")) ///
    scheme(s2color)
graph export "output/cases/figures/case09_06_cluster_visualization.png", replace

/*------------------------------------------------------------------------------
第七部分：聚类命名与解释
------------------------------------------------------------------------------*/

display ""
display "第七部分：聚类命名与解释"
display "------------------------"

* 计算各聚类的平均特征
preserve

collapse (mean) sales_volume profit profit_margin inventory_turnover ///
    customer_rating return_rate market_share sales_growth ///
    (count) n=product_id, by(product_cluster)

list

* 基于特征为聚类命名
gen cluster_name = ""
gen cluster_strategy = ""

* 需要根据实际数据调整命名逻辑
* 这里提供一个示例框架

forvalues i = 1/4 {
    local avg_profit = profit[`i']
    local avg_growth = sales_growth[`i']
    local avg_rating = customer_rating[`i']
    
    if `avg_profit' > 5000 & `avg_growth' > 0 {
        replace cluster_name = "明星产品" if product_cluster == `i'
        replace cluster_strategy = "重点投资，扩大市场" if product_cluster == `i'
    }
    else if `avg_profit' > 5000 & `avg_growth' <= 0 {
        replace cluster_name = "现金牛产品" if product_cluster == `i'
        replace cluster_strategy = "维持现状，优化成本" if product_cluster == `i'
    }
    else if `avg_profit' <= 5000 & `avg_growth' > 0 {
        replace cluster_name = "问题产品" if product_cluster == `i'
        replace cluster_strategy = "评估潜力，选择性投资" if product_cluster == `i'
    }
    else {
        replace cluster_name = "瘦狗产品" if product_cluster == `i'
        replace cluster_strategy = "考虑淘汰或转型" if product_cluster == `i'
    }
}

display ""
display "聚类命名和策略："
display "==================="
list product_cluster cluster_name cluster_strategy n, clean

restore

* 将聚类名称合并回原数据
gen cluster_name = ""
gen cluster_strategy = ""

* 手动设置（基于上面的分析结果）
* 注意：实际应用中需要根据数据调整

quietly summarize profit if product_cluster == 1
local avg_profit_1 = r(mean)
quietly summarize sales_growth if product_cluster == 1
local avg_growth_1 = r(mean)

quietly summarize profit if product_cluster == 2
local avg_profit_2 = r(mean)
quietly summarize sales_growth if product_cluster == 2
local avg_growth_2 = r(mean)

quietly summarize profit if product_cluster == 3
local avg_profit_3 = r(mean)
quietly summarize sales_growth if product_cluster == 3
local avg_growth_3 = r(mean)

quietly summarize profit if product_cluster == 4
local avg_profit_4 = r(mean)
quietly summarize sales_growth if product_cluster == 4
local avg_growth_4 = r(mean)

* 简化命名逻辑
replace cluster_name = "高利润产品" if product_cluster == 1
replace cluster_name = "成长型产品" if product_cluster == 2
replace cluster_name = "稳定型产品" if product_cluster == 3
replace cluster_name = "待优化产品" if product_cluster == 4

replace cluster_strategy = "重点投资" if product_cluster == 1
replace cluster_strategy = "培育发展" if product_cluster == 2
replace cluster_strategy = "维持现状" if product_cluster == 3
replace cluster_strategy = "优化或淘汰" if product_cluster == 4

/*------------------------------------------------------------------------------
第八部分：产品组合分析
------------------------------------------------------------------------------*/

display ""
display "第八部分：产品组合分析"
display "----------------------"

* 1. 各聚类的规模和贡献
display ""
display "1. 各聚类的规模和贡献"
display "----------------------"

preserve
collapse (count) product_count=product_id ///
    (sum) total_revenue=revenue total_profit=profit ///
    (mean) avg_profit_margin=profit_margin avg_customer_rating=customer_rating, ///
    by(cluster_name)

gen revenue_share = total_revenue / sum(total_revenue) * 100
gen profit_share = total_profit / sum(total_profit) * 100

list cluster_name product_count total_revenue total_profit ///
    revenue_share profit_share avg_profit_margin avg_customer_rating, clean

restore

* 2. 产品组合矩阵可视化
graph bar (count), over(cluster_name) ///
    title("各类产品数量分布") ///
    ytitle("产品数量") ///
    blabel(bar, format(%9.0f)) ///
    scheme(s2color)
graph export "output/cases/figures/case09_07_cluster_count.png", replace

* 3. 各类产品的平均利润
graph bar (mean) profit, over(cluster_name) ///
    title("各类产品平均利润") ///
    ytitle("平均月利润（美元）") ///
    blabel(bar, format(%9.0f)) ///
    scheme(s2color)
graph export "output/cases/figures/case09_08_avg_profit_by_cluster.png", replace

* 4. 各类产品的客户评分
graph bar (mean) customer_rating, over(cluster_name) ///
    title("各类产品平均客户评分") ///
    ytitle("平均评分") ///
    blabel(bar, format(%9.2f)) ///
    scheme(s2color)
graph export "output/cases/figures/case09_09_rating_by_cluster.png", replace

/*------------------------------------------------------------------------------
第九部分：优化建议
------------------------------------------------------------------------------*/

display ""
display "第九部分：优化建议"
display "------------------"

* 1. 识别需要淘汰的产品
display ""
display "1. 需要淘汰的产品（低利润 + 低增长 + 低评分）"
display "----------------------------------------------"

gen should_eliminate = (profit < 2000 & sales_growth < -0.1 & customer_rating < 3.5)

list product_id product_name profit sales_growth customer_rating cluster_name ///
    if should_eliminate, clean

count if should_eliminate
display "建议淘汰产品数量: " r(N)

* 2. 识别需要重点投资的产品
display ""
display "2. 需要重点投资的产品（高利润 + 高增长）"
display "------------------------------------------"

gen should_invest = (profit > 5000 & sales_growth > 0.05)

list product_id product_name profit sales_growth market_share cluster_name ///
    if should_invest, clean

count if should_invest
display "建议重点投资产品数量: " r(N)

* 3. 产品组合优化建议
display ""
display "3. 产品组合优化建议"
display "--------------------"

gen optimization_action = ""
replace optimization_action = "重点投资" if should_invest
replace optimization_action = "考虑淘汰" if should_eliminate
replace optimization_action = "维持现状" if optimization_action == "" & profit >= 3000
replace optimization_action = "优化改进" if optimization_action == ""

tabulate optimization_action

* 4. 预期影响分析
display ""
display "4. 预期影响分析"
display "----------------"

* 淘汰低效产品的影响
quietly summarize profit if should_eliminate
local eliminated_profit = r(sum)
display "淘汰产品当前利润贡献: " %10.0f `eliminated_profit' " 美元/月"

* 投资高潜力产品的预期收益（假设增长 30%）
quietly summarize profit if should_invest
local invest_profit = r(sum)
local expected_gain = `invest_profit' * 0.3
display "重点投资产品当前利润: " %10.0f `invest_profit' " 美元/月"
display "预期增量利润（30%增长）: " %10.0f `expected_gain' " 美元/月"

* 净影响
local net_impact = `expected_gain' - `eliminated_profit'
display "净影响: " %10.0f `net_impact' " 美元/月"

/*------------------------------------------------------------------------------
第十部分：保存结果
------------------------------------------------------------------------------*/

display ""
display "第十部分：保存结果"
display "------------------"

* 保存聚类结果
preserve
keep product_id product_name sales_volume revenue profit profit_margin ///
    inventory_turnover customer_rating return_rate market_share sales_growth ///
    product_cluster cluster_name cluster_strategy optimization_action ///
    should_eliminate should_invest
sort product_cluster profit
export delimited using "output/cases/case09_product_clusters.csv", replace
display "聚类结果已保存: output/cases/case09_product_clusters.csv"
restore

* 保存优化建议
preserve
keep if should_eliminate | should_invest
keep product_id product_name profit sales_growth customer_rating ///
    cluster_name optimization_action
sort optimization_action profit
export delimited using "output/cases/case09_optimization_recommendations.csv", replace
display "优化建议已保存: output/cases/case09_optimization_recommendations.csv"
restore

* 保存聚类摘要
preserve
collapse (count) product_count=product_id ///
    (sum) total_revenue=revenue total_profit=profit ///
    (mean) avg_sales=sales_volume avg_profit=profit avg_margin=profit_margin ///
    avg_rating=customer_rating avg_return=return_rate, ///
    by(cluster_name cluster_strategy)
export delimited using "output/cases/case09_cluster_summary.csv", replace
display "聚类摘要已保存: output/cases/case09_cluster_summary.csv"
restore

/*------------------------------------------------------------------------------
总结
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "案例 9 完成！"
display "=========================================="
display ""
display "主要成果："
display "1. 完成产品聚类分析（K-means，k=4）"
display "2. 使用 PCA 降维可视化产品组合"
display "3. 识别 4 类产品：高利润、成长型、稳定型、待优化"
display "4. 提供产品组合优化建议"
display "5. 生成 9 个可视化图表"
display ""
display "关键发现："
display "- 建议淘汰产品数量: " r(N)
display "- 建议重点投资产品数量: (见上文)"
display "- 预期净影响: " %10.0f `net_impact' " 美元/月"
display ""
display "优化建议："
display "1. 淘汰低利润、低增长、低评分产品"
display "2. 重点投资高利润、高增长产品"
display "3. 优化中等产品的运营效率"
display "4. 集中资源于核心产品线"
display ""
display "输出文件："
display "- 图表: output/cases/figures/case09_*.png (9个)"
display "- 聚类结果: output/cases/case09_product_clusters.csv"
display "- 优化建议: output/cases/case09_optimization_recommendations.csv"
display "- 聚类摘要: output/cases/case09_cluster_summary.csv"
display "- 日志: output/cases/case09_product_portfolio.log"
display ""

log close

